package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.system.util.ArrayUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminGroupLink;
import manage.model.AdminLogin;

import java.util.List;

@ActionMeta(name="manage/pageUtil")
public class PageUtilAction extends ManageAction {

	@RequestMeta(params= {"orgGroup","roleGroup"})
	public JSONMessage getAdminLink(String orgGroup,String roleGroup) {
		JSONMessage result=new JSONMessage();
		try {
			AdminLogin admin=getSessionAdmin();
			if(null==admin) throw noLoginException;
			ModelQueryList inQueryList;
			if(!StringUtil.isSpace(orgGroup)) {
				String[] nodes=ArrayUtil.toNoEmptyArray(StringUtil.noSpace(orgGroup).split(","));
				inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null, 
					QueryCondition.and(
						QueryCondition.eq("adminGroup.type", "C"),
						QueryCondition.or(
							QueryCondition.in("adminGroup.oid", nodes),
							QueryCondition.in("adminGroup.num", nodes),
							QueryCondition.in("adminGroup.parent.oid", nodes),
							QueryCondition.in("adminGroup.parent.num", nodes)
						)
					)
				);
			}else {
				inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null, 
					QueryCondition.eq("adminGroup.type", "C")
				);
			}
			if(!StringUtil.isSpace(roleGroup)) {
				String[] flter=ArrayUtil.toNoEmptyArray(StringUtil.noSpace(roleGroup).split(","));
				inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null, 
					QueryCondition.and(
						QueryCondition.eq("adminGroup.type", "B"),
						QueryCondition.or(
							QueryCondition.in("adminGroup.oid", flter),
							QueryCondition.in("adminGroup.num", flter)
						),
						QueryCondition.in("admin.oid", inQueryList)
					)
				);
//			}else {
//				inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null,
//					QueryCondition.and(
//						QueryCondition.eq("adminGroup.type", "B"),
//						QueryCondition.in("admin.oid", inQueryList)
//					)
//				);
			}
			List<AdminGroupLink> list=ModelQueryList.getModelList(AdminGroupLink.class, 
				new String[] {"*","admin.*","adminGroup.*","adminGroup.parent.*"},null, 
				QueryCondition.and(
					QueryCondition.eq("admin.status", "0"),
					QueryCondition.in("admin.oid",inQueryList)
				),
				QueryOrder.asc("adminGroup.sort")
			);
			result.push("list", list);
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}

}
